<script setup>
	import {
		ref
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'

	// 表单数据
	const formData = ref({
		username: '',
		password: ''
	})

	// 加载状态
	const loading = ref(false)

	// 登录页跳转
	const navigateToLogin = () => {
		uni.navigateTo({
			url: '/pages/login/login'
		});
	};

	/**
	 * 验证密码强度 (修改后匹配测试用例要求)
	 */
	const validatePassword = (password) => {
		// 只需包含大小写字母，根据测试用例要求
		const reg = /^(?=.*[a-z])(?=.*[A-Z]).+$/
		return reg.test(password)
	}

	/**
	 * 验证手机号格式 (整合后的单一函数)
	 */
	const validatePhone = (phone) => {
		if (!phone) return 'empty'; // 手机号为空
		if (!/^\d+$/.test(phone)) return 'notNumber'; // 非纯数字
		if (phone.length !== 11) return 'not11Digits'; // 不是11位
		if (!/^1[3-9]\d{9}$/.test(phone)) return 'invalidFormat'; // 格式不正确
		return 'valid';
	}

	// 注册函数
	const handleRegister = async () => {
		// 验证手机号
		const phoneValidation = validatePhone(formData.value.username);

		switch (phoneValidation) {
			case 'empty':
				uni.showToast({
					title: '手机号不能为空',
					icon: 'none'
				});
				return;
			case 'notNumber':
				uni.showToast({
					title: '手机号需为纯数字',
					icon: 'none'
				});
				return;
			case 'not11Digits':
				uni.showToast({
					title: '手机号需为11位',
					icon: 'none'
				});
				return;
			case 'invalidFormat':
				uni.showToast({
					title: '请输入正确的11位手机号',
					icon: 'none'
				});
				return;
		}

		// 验证密码
		if (!formData.value.password) {
			uni.showToast({
				title: '请输入密码',
				icon: 'none'
			});
			return;
		}

		if (!validatePassword(formData.value.password)) {
			uni.showToast({
				title: '密码需包含大小写字母',
				icon: 'none',
				duration: 3000
			});
			return;
		}

		loading.value = true;
		try {
			const res = await uni.request({
				url: 'http://localhost:8080/api/user/register',
				method: 'POST',
				data: formData.value,
				header: {
					'Content-Type': 'application/json'
				}
			});

			const response = res[1] || res;
			console.log('后端响应:', response.data);

			if (response.statusCode === 200 && response.data.code === 200) {
				uni.showToast({
					title: '注册成功',
					icon: 'success'
				});
				setTimeout(() => navigateToLogin(), 1500);
			} else {
				uni.showToast({
					title: response.data?.message || '注册失败',
					icon: 'none'
				});
			}
		} catch (error) {
			console.error('注册出错:', error);
			uni.showToast({
				title: '网络错误',
				icon: 'none'
			});
		} finally {
			loading.value = false;
		}
	};
</script>
<template>
	<view class="register-page">
		<!-- 轮播图 -->
		<view class="login-page_swiper">
			<swiper circular="true" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
				<swiper-item>
					<image src="../../static/images/swiper1.jpg" mode="aspectFill"></image>
				</swiper-item>
				<swiper-item>
					<image src="../../static/images/swiper2.jpg" mode="aspectFill"></image>
				</swiper-item>
			</swiper>
		</view>

		<!-- 注册表单部分 -->
		<view class="register-form">
			<view class="form-item">
				<text class="form-label">手机号</text>
				<u-input v-model="formData.username" class="form-input" type="number" placeholder="请输入11位手机号"
					maxlength="11" />
			</view>
			<view class="form-item">
				<text class="form-label">密码</text>
				<u-input v-model="formData.password" class="form-input" type="password" :password-icon="true"
					placeholder="密码需8位以上含大小写字母和数字" maxlength="20" />
			</view>

			<button class="register-btn" :disabled="loading" :loading="loading" @click="handleRegister">
				{{ loading ? '注册中...' : '立即注册' }}
			</button>

			<view class="login-link">
				已有账号？<text @click="navigateToLogin">去登录</text>
			</view>
		</view>
	</view>
</template>

<style scoped lang="scss">
	.register-page {
		width: 100%;
		min-height: 100vh;
		background-color: #fff;

		.login-page_swiper {
			width: 100%;

			swiper {
				width: 100%;
				height: 440rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}
		}

		.register-form {
			padding: 60rpx 60rpx;

			.form-item {
				display: flex;
				align-items: center;
				margin-bottom: 40rpx;
				padding-bottom: 20rpx;
				border-bottom: 1rpx solid #eee;

				.form-label {
					width: 120rpx;
					font-size: 32rpx;
					color: #333;
				}

				.form-input {
					flex: 1;
					font-size: 32rpx;
				}
			}

			.register-btn {
				margin-top: 60rpx;
				width: 100%;
				height: 90rpx;
				line-height: 90rpx;
				text-align: center;
				font-size: 34rpx;
				font-weight: 500;
				color: #fff;
				background-color: #ff6a6c;
				border-radius: 45rpx;
				box-shadow: 0 4rpx 12rpx rgba(255, 106, 108, 0.3);

				&[disabled] {
					opacity: 0.6;
				}

				&:active:not([disabled]) {
					opacity: 0.8;
				}
			}

			.login-link {
				margin-top: 40rpx;
				text-align: center;
				font-size: 28rpx;
				color: #999;

				text {
					color: #ff6a6c;
					margin-left: 10rpx;
				}
			}
		}
	}
</style>